package com.ddc.study.feature.collect.article;

import com.ddc.study.R;
import com.ddc.study.entity.Collection;
import com.ddc.study.entity.MainData;
import com.ddc.study.net.NetworkUtil;
import com.ddc.study.net.response.BaseResponse;

import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created on 2018/5/14.
 *
 * @author lshun
 * @version 1.0
 */
class CollectArticlePresenter extends CollectArticleContract.IPresenter {
    @Override
    void loadData(@IntRange(from = 1) final int page) {
        if (page <= 0) {
            getView().loadDataFailed("page must > 0");
            return;
        }

        NetworkUtil.getCollectArticle(page).enqueue(new Callback<BaseResponse<MainData<Collection>>>() {
            @Override
            public void onResponse(@NonNull Call<BaseResponse<MainData<Collection>>> call,
                                   @NonNull Response<BaseResponse<MainData<Collection>>> response) {

                if (!isViewAttached()) return;
                if (!response.isSuccessful()) {
                    getView().loadDataFailed("请求失败(" + response.code() + ")");
                    return;
                }
                BaseResponse<MainData<Collection>> body = response.body();
                if (body == null) {
                    getView().loadDataFailed(getView().getStringValue(R.string.response_empty_body));
                } else if (!body.isSuccess()) {
                    getView().loadDataFailed(body.getErrorMsg());
                } else {
                    getView().loadDataSuccess(body.getData());
                }
            }

            @Override
            public void onFailure(@NonNull Call<BaseResponse<MainData<Collection>>> call,
                                  @NonNull Throwable t) {
                if (isViewAttached()) {
                    getView().loadDataFailed(t.getMessage());
                }
            }
        });
    }

    @Override
    void unCollectArticle(final int id, final int originId) {
        Runnable runnable = () -> {
            final boolean result = NetworkUtil.unCollectArticle2(id, originId);

            if (!isViewAttached()) return;
            getView().runOnUIThread(() -> {
                if (isViewAttached()) {
                    getView().showUnCollectResult(result, id, originId);
                }
            });
        };

        mThreadPool.submit(runnable);
    }

    @Override
    void addCollect(final String title, final String author, final String link) {
        Runnable runnable = () -> {
            final boolean result = NetworkUtil.collectOuterArticle(title, author, link);

            if (!isViewAttached()) return;
            getView().runOnUIThread(() -> {
                if (isViewAttached()) {
                    getView().showAddCollectResult(result);
                }
            });
        };

        mThreadPool.submit(runnable);
    }
}
